﻿using System;
using System.Collections.Generic;

namespace EnterpriseFramework.DataAccess
{
    public class Table
    {
        private string name = string.Empty;
        private string schema = string.Empty;
        private bool readOnly = false;

        private AbstractColumn[] columns;
        private AbstractColumn[] primaryColumns;

        private AbstractColumn[] uniqueColumns;

        public AbstractColumn[] UniqueColumns
        {
            get { return uniqueColumns; }
            set { uniqueColumns = value; }
        }
        private AbstractColumn autoColumn;

        /// <summary>
        /// Creates a new table model.
        /// </summary>
        /// <param name="name">name of the table</param>
        /// <param name="schema">schema to which this table belongs</param>
        /// <param name="sequence">auto number generating sequence</param>
        /// <param name="columns">array of columns for this table</param>
        /// <param name="triggers">array of triggers for this table</param>
        /// <param name="readOnly">whether this table is readonly</param>
        public Table(string name, string schema, AbstractColumn[] columns, bool readOnly)
        {
            this.name = name;
            this.schema = schema;
            this.readOnly = readOnly;
            this.columns = columns;

            List<AbstractColumn> pk = new List<AbstractColumn>();
            List<AbstractColumn> Uniquekeys = new List<AbstractColumn>();

            foreach (AbstractColumn c in columns)
            {
                if (c.PrimaryKey)
                    pk.Add(c);
                if (c.IsUnique)
                    Uniquekeys.Add(c);
                if (c.AutoIncrement)
                {
                    if (autoColumn == null)
                        autoColumn = c;
                    //else
                    //throw new DeclarationException("A table cannot have multiple AutoIncrement columns.");
                }
            }

            primaryColumns = pk.ToArray();
            uniqueColumns = Uniquekeys.ToArray();
        }

        public AbstractColumn[] PrimaryColumns
        {
            get { return primaryColumns; }
            set { primaryColumns = value; }
        }

        public AbstractColumn[] Columns
        {
            get { return columns; }
            set { columns = value; }
        }

        public string Name { get { return name; } set { name = value; } }

        public string Schema { get { return schema; } set { name = value; } }

        public bool ReadOnly { get { return readOnly; } set { readOnly = value; } }
    }
}
